package com.lcxw.hxzb.friendcircle.framework.schedul;
import com.lcxw.hxzb.friendcircle.common.utils.Arith;
import com.lcxw.hxzb.friendcircle.project.system.domain.YwpdLoves;
import com.lcxw.hxzb.friendcircle.project.system.domain.YwpdLovesMoneyDetail;
import com.lcxw.hxzb.friendcircle.project.system.mapper.YwpdLovesMapper;
import com.lcxw.hxzb.friendcircle.project.system.mapper.YwpdLovesMoneyDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Component
@EnableScheduling   // 1.开启定时任务
public class LovesSch {

    @Autowired
    YwpdLovesMapper ywpdLovesMapper;
    @Autowired
    YwpdLovesMoneyDetailMapper ywpdLovesMoneyDetailMapper;


    /**
     * 情侣cp等级规则
     * @throws InterruptedException
     */
//    @Scheduled(cron = "0 */1 * * * ?")//每隔一分钟执行一次
    @Scheduled(cron = "0 0 0 * * ?")//每天凌晨0点执行一次
    @Transactional
    public void first() throws InterruptedException {
        List<YwpdLoves> lists= ywpdLovesMapper.selectLovesList();
        if(!lists.isEmpty()){
            for(YwpdLoves ywpdLoves:lists){
                Double rate=0.0;
                int level=Integer.valueOf(ywpdLoves.getName());
                if(level<=10){
                    rate=0.1;
                }
                if(level>10&&level<=20){
                    rate=0.05;
                }
                if(level>20&&level<=38){
                    rate=0.03;
                }
                double reduce= Arith.mul(rate,ywpdLoves.getMoney().doubleValue());
                ywpdLoves.setMoney(new BigDecimal(Arith.sub(ywpdLoves.getMoney().doubleValue(),reduce)));
                ywpdLovesMapper.updateYwpdLoves(ywpdLoves);
                YwpdLovesMoneyDetail detail=new YwpdLovesMoneyDetail();
                detail.setMoney(BigDecimal.valueOf(-reduce));
                detail.setCreateTime(new Date());
                detail.setLovesId(ywpdLoves.getLovesId());
                detail.setType("8");
                ywpdLovesMoneyDetailMapper.insertYwpdLovesMoneyDetail(detail);
                System.out.println(reduce+"----------------------");
            }
        }
    }

}
